package com.zatech.cgnci.project.controller.claim;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zatech.cgnci.project.base.context.UserContext;
import com.zatech.cgnci.project.base.enums.ClaimCaseStatusEnum;
import com.zatech.cgnci.project.base.utils.VerifyUtils;
import com.zatech.cgnci.project.dto.CgnciClaimCaseQueryDTO;
import com.zatech.cgnci.project.entity.UserEntity;
import com.zatech.cgnci.project.service.CgnciClaimCaseTaskService;
import com.zatech.cgnci.project.service.CgnciClaimWorkbenchService;
import com.zatech.cgnci.project.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/claim/workbench")
@RequiredArgsConstructor
@Api(produces = "application/json", tags = "理赔案件工作台接口")
public class CgnciClaimWorkbenchController {

    private final CgnciClaimWorkbenchService workbenchService;
    private final CgnciClaimCaseTaskService cgnciClaimCaseTaskService;

    @PostMapping("/page")
    @ApiOperation(value = "分页查询-我的案件")
    public IPage<CgnciClaimCasePageWorkbenchVO> page(@RequestBody CgnciClaimCaseQueryDTO queryDTO) {
        // 获取当前登录用户
        UserEntity user = UserContext.getUser();
        queryDTO.setCreateUser(user.getNameAndCode());
        IPage<CgnciClaimCasePageWorkbenchVO> page = workbenchService.page(queryDTO);
        List<CgnciClaimCasePageWorkbenchVO> records = page.getRecords();
        List<String> caseStatus = new ArrayList<>();
        caseStatus.add(ClaimCaseStatusEnum.CLAIM.getCode());
        caseStatus.add(ClaimCaseStatusEnum.REOPEN.getCode());
        if (VerifyUtils.isNotEmpty(records)) {
            records.forEach(record -> record.setHasEdit(caseStatus.contains(record.getCaseStatus())));
        }
        return page;
    }

    @GetMapping("/detail")
    @ApiOperation(value = "案件列表任务明细查询")
    public List<CgnciClaimCaseTaskVO> claimCaseDetail(@RequestParam(value = "caseNo") @NotNull(message = "caseNo不能为空") String caseNo) {
        UserEntity user = UserContext.getUser();
        if (VerifyUtils.isEmpty(user)) {
            return null;
        }
        return workbenchService.claimCaseDetail(caseNo);
    }

    @PostMapping("/casePageTask")
    @ApiOperation(value = "分页查询-我的任务")
    public IPage<CgnciClaimCasePageWorkbenchVO> casePageTask(@RequestBody CgnciClaimCaseQueryDTO req) {
        // 获取当前登录用户
        UserEntity user = UserContext.getUser();
        req.setCreateUser(user.getNameAndCode());
        IPage<CgnciClaimCasePageWorkbenchVO> page = cgnciClaimCaseTaskService.casePageTaskWorkbench(req);
        List<CgnciClaimCasePageWorkbenchVO> records = page.getRecords();
        if (VerifyUtils.isNotEmpty(records)) {
            records.forEach(record -> record.setHasEdit(VerifyUtils.isNotEmpty(record.getCurrentApproverAndName()) && record.getCurrentApproverAndName().contains(user.getNameAndCode())));
        }
        return page;
    }

    @PostMapping("/caseTaskCount")
    @ApiOperation(value = "我的案件和任务数查询")
    public CgnciClaimCaseTaskCountVO caseTaskCount() {
        UserEntity user = UserContext.getUser();
        if (VerifyUtils.isEmpty(user)) {
            return null;
        }
        return cgnciClaimCaseTaskService.caseTaskCount();
    }


    @PostMapping("/caseTaskQuantity")
    @ApiOperation(value = "我的任务table查询")
    public CgnciClaimCaseQuantityVO caseTaskQuantity() {
        UserEntity user = UserContext.getUser();
        if (VerifyUtils.isEmpty(user)) {
            return null;
        }
        return cgnciClaimCaseTaskService.caseTaskQuantity();
    }


    @GetMapping("/caseStatusCount")
    @ApiOperation(value = "我的任务状态数量")
    public CaseStatusCountVO caseStatusCount(@RequestParam(value = "taskType") @NotNull(message = "taskType不能为空") String taskType) {
        UserEntity user = UserContext.getUser();
        if (VerifyUtils.isEmpty(user)) {
            return null;
        }
        return cgnciClaimCaseTaskService.caseStatusCount(taskType);
    }
}
